/***************************************************************************
* filename:Led.c
* Led driver for qingfeng nrf51822 broad
* sunx 2016.10.14
*
*****************************************************************************/
#include "Led.h"



#define APP_TIMER_PRESCALER  0              
APP_TIMER_DEF(m_led_test_timer_id); 
#define LED_TEST_DETECTED_INTERVAL APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER)  /**< Sensor Contact Detected toggle interval (ticks). */

ledevent ledtestevent;

/****************************************************************
*name:	LedTestEventInit()
*func:	led test proc func
*
*return:	none
*/
void LedTestEventInit(void)
{
	uint32_t err_code;
	ledtestevent.event_total_times=200;
	ledtestevent.event_led_idex=0;
	err_code = app_timer_create(&m_led_test_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                led_test_proc_timeout_handler);
    APP_ERROR_CHECK(err_code);

}
/****************************************************************
*name:LedTestEventStart()
*func:led test proc func
*
* 2016.10.20
*/
void LedTestEventStart()
{
	uint32_t err_code;
	err_code = app_timer_start(m_led_test_timer_id, LED_TEST_DETECTED_INTERVAL, NULL);
    APP_ERROR_CHECK(err_code);
}
/****************************************************************
*name:LedTestProc()
*func:led test proc func
*
* 2016.10.20
*/
void LedTestProc()
{
    switch(ledtestevent.event_led_idex)
    {
		case 0:
			if(LED_PIO_IS_ON(LED_2))
				{
					
					LED_PIO_OFF(LED_2);
				}
			else
				{
					//high is led off
					LED_PIO_ON(LED_2);
				}
			ledtestevent.event_led_idex++;
			break;
		case 1:
			if(LED_PIO_IS_ON(LED_3))
				{
					LED_PIO_OFF(LED_3);
				}
			else
				{
					LED_PIO_ON(LED_3);
				}
			ledtestevent.event_led_idex++;
			break;
		case 2:
			if(LED_PIO_IS_ON(LED_4))
				{
					LED_PIO_OFF(LED_4);
				}
			else
				{
					LED_PIO_ON(LED_4);
				}
			ledtestevent.event_led_idex=0;
			break;
		default:
			break;
	}
	if(ledtestevent.event_total_times!=0)
	{
		ledtestevent.event_total_times--;
	}
	else
	{
		//end event loop
		app_timer_stop(m_led_test_timer_id);
		//all off
		LED_PIO_OFF(LED_1);
		LED_PIO_OFF(LED_2);
		LED_PIO_OFF(LED_3);
		LED_PIO_OFF(LED_4);
	}
	#ifdef DEBUG_LED_OPEN
	printf("LED:ledtestevent.event_total_times=%d",ledtestevent.event_total_times);
	#endif
}
/****************************************************************
*name:led_test_proc_timeout_handler()
*func:led test proc
*
* 2016.10.20
*/
static void led_test_proc_timeout_handler(void * p_context)
{
    UNUSED_PARAMETER(p_context);

    LedTestProc();
}
/****************************************************************
*name:Init_Leds()
*func:led pio init for qing feng
*
* 2016.10.20
*/
void Init_Leds()

{
	//init output
	LED_PIO_CONFIGURE(LED_1);
	LED_PIO_CONFIGURE(LED_2);
	LED_PIO_CONFIGURE(LED_3);
	LED_PIO_CONFIGURE(LED_4);
	//all off
	LED_PIO_OFF(LED_1);
	LED_PIO_OFF(LED_2);
	LED_PIO_OFF(LED_3);
	LED_PIO_OFF(LED_4);
}
